import { defineStore } from 'pinia';
import { ref } from 'vue';
import type { customerInfoType } from '@/types/login';

export const useStore = defineStore(
  'user',
  () => {
    // 用户信息
    const userInfo = ref<customerInfoType>();

    const setUserInfo = (value: customerInfoType) => {
      userInfo.value = value;
    };

    // 清理用户信息
    const clearUserInfo = () => {
      userInfo.value = undefined;
    };

    return {
      userInfo,
      setUserInfo,
      clearUserInfo,
    };
  },
  // 持久化
  {
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key);
        },
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
      },
    },
  },
);
